package com.mapler.mayzus.ui;

import com.mapler.utility.HttpHelper;
import com.mapler.utility.IConstant;
import java.awt.Color;
import java.net.URL;
import java.net.URLEncoder;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;

/**
 *
 * @author none
 */
public class AddMayzusAccUI extends javax.swing.JPanel {

    private String username;
    private String password;
    private String country;
    
    private static Logger log = Logger.getLogger(AddMayzusAccUI.class);
    public AddMayzusAccUI() {
        initComponents();
    }
    
     public AddMayzusAccUI(String username, String password,String country) {
         this.username = username;
         this.password = password;
         this.country = country;
         initComponents();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        message = new javax.swing.JLabel();
        email = new javax.swing.JTextField();
        jLabelEmail = new javax.swing.JLabel();
        emailPassword = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        forwardEmail = new javax.swing.JTextField();
        forwardEmailPass = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        ngfxusername = new javax.swing.JTextField();
        ngfxpassword = new javax.swing.JTextField();
        verify_code1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        verify_code2 = new javax.swing.JTextField();
        dateofbirth = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        Add = new javax.swing.JButton();

        message.setText("Message");

        jLabelEmail.setLabelFor(email);
        jLabelEmail.setText("Email");

        jLabel1.setText("Email Password");

        jLabel2.setText("Forward email");

        jLabel3.setText("Forwar email password");

        jLabel4.setText("Username");

        jLabel5.setText("Password");

        jLabel6.setText("Verify code1");

        jLabel7.setText("Verify code2");

        jLabel8.setText("Date of Birth");

        Add.setText("Add");
        Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 429, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabelEmail)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(dateofbirth)
                            .addComponent(verify_code2)
                            .addComponent(verify_code1)
                            .addComponent(ngfxpassword)
                            .addComponent(ngfxusername)
                            .addComponent(forwardEmailPass, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                            .addComponent(forwardEmail)
                            .addComponent(emailPassword)
                            .addComponent(email)))
                    .addComponent(Add, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(89, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelEmail))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(forwardEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(forwardEmailPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ngfxusername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ngfxpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(verify_code1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(verify_code2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dateofbirth, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                .addComponent(Add)
                .addGap(0, 15, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddActionPerformed
       try {
            StringBuilder request = new StringBuilder();
            request.append("<Request>");
            request.append("<username>").append(this.username).append("</username>");
            request.append("<password>").append(this.password).append("</password>");
            request.append("<email>").append(this.email.getText()).append("</email>");
            request.append("<email_pass>").append(this.emailPassword.getText()).append("</email_pass>");
            request.append("<fw_email>").append(this.forwardEmail.getText()).append("</fw_email>");
            request.append("<fw_email_pass>").append(this.forwardEmailPass.getText()).append("</fw_email_pass>");
            request.append("<mayzus_username>").append(this.ngfxusername.getText()).append("</mayzus_username>");
            request.append("<mayzus_username>").append(this.ngfxpassword.getText()).append("</mayzus_username>");
            request.append("<verify_code1>").append(this.verify_code1.getText()).append("</verify_code1>");
            request.append("<verify_code2>").append(this.verify_code2.getText()).append("</verify_code2>");
            request.append("<date_of_birth>").append(this.dateofbirth.getText()).append("</date_of_birth>");
            request.append("</Request>");
            
            String req = URLEncoder.encode(request.toString(), "UTF-8");
            String uri = "http://" + IConstant.HOSTNAME + "/index.php?r=mayzusAccount/addAccount";
            String param = "request=" + req;
            String response = HttpHelper.post(new URL(uri), param);
            
            Document document = DocumentHelper.parseText(response);
            String errorCode = document.valueOf("Response/errorcode");
            String msg = document.valueOf("Response/message");
            if (errorCode.equalsIgnoreCase("000")) {                
                this.message.setBackground(Color.red);
            } else{
               this.message.setBackground(Color.black); 
            }
            this.message.setText(msg);
        } catch (Exception ex) {
            this.message.setText("Error "+ex.getMessage());
            this.message.setText(ex.getMessage());
        }
    }//GEN-LAST:event_AddActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Add;
    private javax.swing.JTextField dateofbirth;
    private javax.swing.JTextField email;
    private javax.swing.JTextField emailPassword;
    private javax.swing.JTextField forwardEmail;
    private javax.swing.JTextField forwardEmailPass;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabelEmail;
    private javax.swing.JLabel message;
    private javax.swing.JTextField ngfxpassword;
    private javax.swing.JTextField ngfxusername;
    private javax.swing.JTextField verify_code1;
    private javax.swing.JTextField verify_code2;
    // End of variables declaration//GEN-END:variables

    public void clearFields(){
        try{
            verify_code1.setText("");
            email.setText("");
            emailPassword.setText("");
            forwardEmail.setText("");
            forwardEmailPass.setText("");
            verify_code2.setText("");
            ngfxusername.setText("");
            dateofbirth.setText("");
            ngfxpassword.setText("");
            message.setText("");
        }catch( Throwable ex ) {
            ex.printStackTrace();
            log.debug("clearFields:: Error causes "+ex);
        }
    }
}
